package com.diandian.backend.client.controller;

import com.diandian.backend.client.param.*;
import com.diandian.backend.client.resp.ClientInfoDetailResp;
import com.diandian.backend.client.service.ClientInfoService;
import com.diandian.backend.client.service.ClientService;
import com.diandian.base.boot.starter.response.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/client")
public class ClientController {

    private static final Logger logger = LoggerFactory.getLogger(ClientController.class);

    @Autowired
    private ClientService clientService;

    @Autowired
    private ClientInfoService clientInfoService;

    /**
     * 手动权限缓存过程
     */
    @GetMapping("/cache/manual")
    public BaseResult manualCache(@RequestParam(value = "clientId") String clientId) {
        logger.info("执行手动权限缓存，clientId:{}", clientId);
        clientService.manualCache(clientId);
        return new BaseResult(0, "success", null);
    }

    @PostMapping("/function/setting/page")
    public BaseResult functionSettingPage(@RequestBody ClientFunctionSettingPageParam param) {
        param.check();
        return new BaseResult(0, "success", clientInfoService.functionSettingPage(param.getPageNum(), param.getPageSize(), param.getClientId()));
    }

    @PostMapping("/page")
    public BaseResult page(@RequestBody ClientClientInfoPageParam param) {
        param.check();
        return new BaseResult(0, "success", clientInfoService.page(param.toDTO()));
    }

    /**
     * 2. 查询详情
     */
    @PostMapping("/detail")
    public BaseResult getDetail(@RequestBody ClientInfoDetailParam param) {
        return new BaseResult(0, "success", clientInfoService.getDetail(param.toDTO()));
    }

    /**
     * 3. 根据数据id更新数据
     */
    @PostMapping("/updateById")
    public BaseResult updateById(@RequestBody ClientClientInfoUpdateByIdParam param) {
        ClientInfoDetailResp detailParamDTO = clientInfoService.updateById(param.toDTO());
        // 同步到redis
        clientService.manualCache(detailParamDTO.getClientId());
        return new BaseResult(0, "success", null);
    }

    /**
     * 4. 根据数据id删除数据
     */
    @PostMapping("/deleteById")
    public BaseResult deleteById(@RequestBody ClientClientInfoDeleteByIdParam param) {
        param.check();
        ClientInfoDetailResp detailParamDTO = clientInfoService.deleteById(param.toDTO());
        // 同步到redis
        clientService.clearCache(detailParamDTO.getClientKey());
        return new BaseResult(0, "success", null);
    }

    /**
     * 5. 新增
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody ClientClientInfoAddParam param) {
        param.check();
        clientInfoService.add(param.toDTO());
        // 同步到redis
        clientService.manualCache(param.getClientId());
        return new BaseResult(0, "success", null);
    }

    /**
     * 7. 列表查询
     */
    @PostMapping("/list")
    public BaseResult list(@RequestBody ClientClientInfoListParam param) {
        return new BaseResult(0, "success", clientInfoService.getList(param.toDTO()));
    }
}
